import 'package:boss/main.dart';
import 'package:boss/models/pay_qr_code.dart';
import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/form/long_text_form_field.dart';
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:boss/ui/widget/shadow_radius_card.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:router/annotation.dart';

@RouterConfig(PayEditDescribePage.pageName)
class PayEditDescribePage extends StatefulWidget {
  static const pageName = '/PayEditDescribePage';

  @Autowired()
  final PayQrCode data;

  PayEditDescribePage(this.data);

  @override
  _PayEditDescribePageState createState() => _PayEditDescribePageState();
}

class _PayEditDescribePageState extends BaseState<PayEditDescribePage> {
  _PayEditDescribePageState() : super(PayEditDescribePage.pageName);

  String describe;

  @override
  void initState() {
    super.initState();

    describe = widget.data.describe;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CenterTitleAppBar(title: '编辑备注信息'),
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 30.w),
              child: ShadowRadiusCard(
                child: LongTextFormField(
                  hintText: '文字数目不超过20字，例如：西区使用',
                  maxLines: 5,
                  maxLength: 20,
                  initValue: describe,
                  onChanged: (value) {
                    describe = value;
                  },
                ),
              ),
            ),
          ),
          FixedBottomBtn(
            title: '保 存',
            onTap: () async {
              if (describe.length > 20) {
                toast('备注不能超过20个字');
                return;
              }
              try {
                showLoadingToast();

                final data = PayQrCode.fromJson(widget.data.toJson());
                data.describe = describe;

                final res = await api.updatePayQrCode(data);
                if (!res.successful()) {
                  toast(res.msg);
                  return;
                }
                toast('修改备注成功');

                Navigator.pop(context, describe);
              } catch (e) {
                toast(e.toString());
              } finally {
                hideLoadingToast();
              }
            },
          ),
        ],
      ),
    );
  }
}
